﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ChuanYe.CountyFireManagement.Api.Models;
using ChuanYe.CountyFireManagement.BLL;
using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ChuanYe.CountyFireManagement.Api.Controllers
{
    /// <summary>
    /// 短信及短信模板
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SMSController : ControllerBase
    {

        /// <summary>
        ///  发送多条短信
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase Add(SmsInfoArrayRequest obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            try
            {
                List<Smsinfo> infos = new List<Smsinfo>();
                if (obj.List != null && obj.List.Count > 0)
                {
                    foreach (var item in obj.List)
                    {
                        Smsinfo info = new Smsinfo();
                        info.SmsContent = item.SmsContent;
                        info.TargetUserId = item.TargetUserId;
                        info.TargetUserName = item.TargetUserName;
                        info.SendUserId = item.SendUserID;
                        info.IsAchieved = item.IsAchieved;
                        info.AchievedDate = item.AchievedDate;
                        info.SendTime = DateTime.Now;
                        info.MobileCode = item.MobileCode;
                        infos.Add(info);
                    }
                }
                result.Status =SmsInfoBLL.Instance.Add(infos);
            }
             catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/SMS/Add Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }

        /// <summary>
        ///  获取短信 分页
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic, dynamic> GetByPage(SmsInfoSearchRequest obj)
        {
            ResultDto<dynamic, dynamic> result = new ResultDto<dynamic, dynamic>();
            try
            {
                var page = Pickout.SetPage(obj);
                return SmsInfoBLL.Instance.GetByPage(obj.SendUserId,obj.TargetUserId,obj.SendDepId,obj.TargetDepId, obj.SearchType,obj.SendAddressCounty, page);
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/SMS/GetByPage Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }

        /// <summary>
        ///  移除短信模板
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase RemoveSmsModel(RemoveSmsModelRequest obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            try
            {
                result.Status = SmsModelBLL.Instance.Remove(obj.ModelId);
                if (result.Status == 0)
                {
                    result.Message = "ModelId Is Not Exist";
                }
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/SMS/RemoveSmsModel Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }

        /// <summary>
        ///  获取短信模板
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic> GetSmsModel()
        {
            ResultDto<dynamic> result = new ResultDto<dynamic>();
            try
            {
                result.FirstParam = SmsModelBLL.Instance.Get();
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/SMS/GetSmsModel Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }

        /// <summary>
        ///  获取短信模板 分页
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic, dynamic> GetSmsModelByPage(RequestPageBase obj)
        {
            ResultDto<dynamic, dynamic> result = new ResultDto<dynamic, dynamic>();
            try
            {
                var page = Pickout.SetPage(obj);
                return SmsModelBLL.Instance.GetByPage(page);
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/SMS/GetSmsModelByPage Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }






        /// <summary>
        ///  增加短信模板
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase AddSmsModel(SmsModelRequest obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            try
            {
                Smsmodel info = new Smsmodel();
                info.Content = obj.Content;
                info.ModelType = obj.ModelType;
                info.AddTime = DateTime.Now;
                result.Status = SmsModelBLL.Instance.Add(info);
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/SMS/AddSmsModel Server Error";
                LogHelper.Error(result.Message, ex);
            }
            return result;
        }
    }
}